package Client端;

import javax.swing.*;
import java.awt.*;

public class LoginMain extends JFrame {
    public static int Width = 400;
    public static int Height = 450;
    public static int TextWidth = 250;
    public static int TextHeight = 40;
    public static int TipWidth = 120;
    public static int TipHeight = TextHeight;
    public static Font font = new Font("", Font.PLAIN, 20);

    public LoginMain() {
        setSize(Width, Height);
        setLocationRelativeTo(null);
        setTitle("初始界面");

        JPanel panel = new JPanel();
        CardLayout cardLayout = new CardLayout();
        panel.setLayout(cardLayout);
        add(panel);

        JPanel LoginPanel = new JPanel();
        LoginPanel.setPreferredSize(new Dimension(Width, Height));
        LoginPanel.setLayout(null);


        JLabel LTitle = new JLabel("登录", JLabel.CENTER);
        LTitle.setFont(new Font("", Font.PLAIN, 30));
        LTitle.setBounds((Width - 200) / 2, 20, 200, 50);
        LoginPanel.add(LTitle);

        JTextField loginId = new JTextField();
        loginId.setBounds(100, 100, TextWidth, TextHeight);
        loginId.setFont(font);
        JLabel loginIdLabel = new JLabel("账号:");
        loginIdLabel.setBounds(20, 100, TipWidth, TextHeight);
        loginIdLabel.setFont(font);
        LoginPanel.add(loginId);
        LoginPanel.add(loginIdLabel);

        JPasswordField loginPassword = new JPasswordField();
        loginPassword.setBounds(100, 100 + TextHeight + 10, TextWidth, TipHeight);
        loginPassword.setFont(font);
        JLabel loginPasswordLabel = new JLabel("密码:");
        loginPasswordLabel.setBounds(20, 100 + TextHeight + 10, TipWidth, TipHeight);
        loginPasswordLabel.setFont(font);
        LoginPanel.add(loginPassword);
        LoginPanel.add(loginPasswordLabel);

        JLabel loginTips = new JLabel();
        loginTips.setBounds(100, 250, 300, 30);
        loginTips.setFont(font);
        LoginPanel.add(loginTips);

        JButton loginSubmit = new JButton("登录");
        loginSubmit.setBounds(80, 300, 100, 40);
        loginSubmit.setFont(font);
        LoginPanel.add(loginSubmit);

        JButton registerBtn = new JButton("注册");
        registerBtn.setBounds(200, 300, 100, 40);
        registerBtn.setFont(font);
        LoginPanel.add(registerBtn);

        loginSubmit.addActionListener(e -> {
            String idStr = loginId.getText();//
            String passwordStr = new String(loginPassword.getPassword());
            if (idStr.length() != 8) {
                loginTips.setText("账号长度为8位！！！");
            } else if (passwordStr.length() != 10) {
                loginTips.setText("密码长度为10位！！！");
            } else if (ClientDeal.checkAccount(idStr, passwordStr)) {
                new ClientFrame(idStr, ClientDeal.getName(idStr));
                setVisible(false);
            } else loginTips.setText("账号密码错误！！！");
        });
        registerBtn.addActionListener(e -> cardLayout.show(panel, "register"));
        panel.add("login", LoginPanel);

        JPanel registerPanel = new JPanel();
        registerPanel.setPreferredSize(new Dimension(Width, Height));
        registerPanel.setLayout(null);

        JLabel RTitle = new JLabel("注册", JLabel.CENTER);
        RTitle.setFont(new Font("", Font.PLAIN, 30));
        RTitle.setBounds((Width - 200) / 2, 20, 200, 50);
        registerPanel.add(RTitle);

        JTextField registerId = new JTextField();
        registerId.setBounds(100, 100, TextWidth, TextHeight);
        registerId.setFont(font);
        JLabel registerIdLabel = new JLabel("账号:");
        registerIdLabel.setBounds(20, 100, TipWidth, TextHeight);
        registerIdLabel.setFont(font);
        registerPanel.add(registerId);
        registerPanel.add(registerIdLabel);

        JPasswordField registerPassword = new JPasswordField();
        registerPassword.setBounds(100, 100 + TextHeight + 10, TextWidth, TextHeight);
        registerPassword.setFont(font);
        JLabel registerPasswordLabel = new JLabel("密码: ");
        registerPasswordLabel.setBounds(20, 100 + TextHeight + 10, TipWidth, TipHeight);
        registerPasswordLabel.setFont(font);
        registerPanel.add(registerPassword);
        registerPanel.add(registerPasswordLabel);

        JTextField registerName = new JTextField();
        registerName.setBounds(100, 100 + (TextHeight + 10) * 2, TextWidth, TextHeight);
        registerName.setFont(font);
        JLabel registerNameLabel = new JLabel("用户名: ");
        registerNameLabel.setBounds(20, 100 + (TextHeight + 10) * 2, TipWidth, TipHeight);
        registerNameLabel.setFont(font);
        registerPanel.add(registerName);
        registerPanel.add(registerNameLabel);

        JLabel registerTips = new JLabel();
        registerTips.setBounds(100, 250, 300, 30);
        registerTips.setFont(font);
        registerPanel.add(registerTips);

        JButton registerSubmit = new JButton("注册");
        registerSubmit.setBounds(80, 300, 100, 40);
        registerSubmit.setFont(font);
        registerPanel.add(registerSubmit);

        JButton loginBtn = new JButton("返回");
        loginBtn.setBounds(200, 300, 100, 40);
        loginBtn.setFont(font);
        registerPanel.add(loginBtn);

        registerSubmit.addActionListener(e -> {
            String idStr = registerId.getText();//
            String passwordStr = new String(registerPassword.getPassword());
            String nameStr = registerName.getText();
            if (idStr.length() != 8) registerTips.setText("账号长度为8位！！！");
            else if (passwordStr.length() != 10) registerTips.setText("密码长度为10位！！！");
            else if (nameStr.isEmpty()) registerTips.setText("用户名不为空");
            else if (ClientDeal.addAccount(idStr, passwordStr, nameStr)) {
                new ClientFrame(idStr, nameStr);
                this.dispose();
            } else registerTips.setText("注册出错！！！");
        });
        loginBtn.addActionListener(e -> cardLayout.show(panel, "login"));
        panel.add("register", registerPanel);
        setVisible(true);
    }

    public static void main(String[] args) {
        new LoginMain();
        //new ClientFrame("12345679", ClientDeal.getName("12345679"));
    }

}